#ifndef _COMMAND_COMMON_H_
#define _COMMAND_COMMON_H_

#include <assert.h>
#include <stdlib.h>
#include <cstddef>
#include <cstring>
#include <string>
#include <stdio.h>

#if defined( __osf__ )
// Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
// what stdint.h would define.
#include <inttypes.h>
#elif !defined( _MSC_VER )
#include <stdint.h>
#endif

#if defined( _MSC_VER ) && defined( USE_DLLS )
	#ifdef LIB_EXPORTS
		#define EXPORT __declspec(dllexport)
	#else
		#define EXPORT __declspec(dllimport)
	#endif
#else
	#define EXPORT
#endif

// exception option
#undef USE_EXCEPTION


#if defined WIN32
#define WINDOWS
#undef LINUX
#undef FREEBSD
#elif defined __linux__
#undef	WINDOWS
#define LINUX
#undef FREEBSD
#elif defined __FreeBSD__
#undef	WINDOWS
#undef	LINUX
#define FREEBSD
#else
#error "Unsupported platform!"
#endif

#ifdef WINDOWS
#define NET_WSA_SELECT
#undef NET_SELECT
#endif

#ifdef WINDOWS
	typedef __int8				int8;
	typedef __int16				int16;
	typedef __int32				int32;
	typedef __int64				int64;
	typedef unsigned __int8		uint8;
	typedef unsigned __int16	uint16;
	typedef unsigned __int32	uint32;
	typedef unsigned __int64	uint64;
#else
	typedef int8_t			int8;
	typedef int16_t			int16;
	typedef int32_t			int32;
	typedef int64_t			int64;
	typedef uint8_t			uint8;
	typedef uint16_t		uint16;
	typedef uint32_t		uint32;
	typedef uint64_t		uint64;
#endif
//tolua_end

// long long macros to be used because gcc and vc++ use different suffixes,
// and different size specifiers in format strings
#undef LONGLONG
#undef ULONGLONG
#undef LL_FORMAT

#ifdef _MSC_VER
#define LONGLONG(x) x##I64
#define ULONGLONG(x) x##UI64
#define LL_FORMAT "I64"  // As in printf("%I64d", ...)
#else
#define LONGLONG(x) x##LL
#define ULONGLONG(x) x##ULL
#define LL_FORMAT "ll"  // As in "%lld". Note that "q" is poor form also.
#endif


static const int32 kint32max = 0x7FFFFFFF;
static const int32 kint32min = -kint32max - 1;
static const int64 kint64max = LONGLONG(0x7FFFFFFFFFFFFFFF);
static const int64 kint64min = -kint64max - 1;
static const uint32 kuint32max = 0xFFFFFFFFu;
static const uint64 kuint64max = ULONGLONG(0xFFFFFFFFFFFFFFFF);

#ifdef _MSC_VER
#define bzero( s, l )	memset( s, 0, l )
#endif

#ifdef LINUX
#define Sleep( t )	usleep( t * 1000 )
#endif

#ifdef WINDOWS
#include "windows.h"
#endif

// -------------------------------------------------------------------
// Annotations:  Some parts of the code have been annotated in ways that might
//   be useful to some compilers or tools, but are not supported universally.
//   You can #define these annotations yourself if the default implementation
//   is not right for you.

#ifndef ATTRIBUTE_ALWAYS_INLINE
#if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
// For functions we want to force inline.
// Introduced in gcc 3.1.
#define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
#else
// Other compilers will have to figure it out for themselves.
#define ATTRIBUTE_ALWAYS_INLINE
#endif
#endif

#ifndef ATTRIBUTE_DEPRECATED
#ifdef __GNUC__
// If the method/variable/type is used anywhere, produce a warning.
#define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
#else
#define ATTRIBUTE_DEPRECATED
#endif
#endif

#ifndef PREDICT_TRUE
#ifdef __GNUC__
// Provided at least since GCC 3.0.
#define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
#else
#define PREDICT_TRUE
#endif
#endif


// A macro to disallow the evil copy constructor and operator= functions
// This should be used in the private: declarations for a class
#undef DISALLOW_EVIL_CONSTRUCTORS
#define DISALLOW_EVIL_CONSTRUCTORS( TypeName )	\
  TypeName( const TypeName& );					\
  void operator=( const TypeName& )

// A macro to disallow all the implicit constructors, namely the
// default constructor, copy constructor and operator= functions.
//
// This should be used in the private: declarations for a class
// that wants to prevent anyone from instantiating it. This is
// especially useful for classes containing only static methods.
#undef DISALLOW_IMPLICIT_CONSTRUCTORS
#define DISALLOW_IMPLICIT_CONSTRUCTORS( TypeName )	\
  TypeName();										\
  DISALLOW_EVIL_CONSTRUCTORS( TypeName )


#undef ALL_SIZE_FUNC
#define ALL_SIZE_FUNC( num, data )	\
	inline int AllSize() const	\
	{	\
		return sizeof( *this ) + (num) * sizeof( data[0] );	\
	}

#undef DATA_SIZE_FUNC
#define DATA_SIZE_FUNC( num, data )	\
	inline int DataSize() const	\
	{	\
		return (num) * sizeof( data[0] );	\
	}

#define STRUCT_CONSTRUCTOR( name )	\
	name()	\
	{	\
		bzero( this, sizeof( *this ) );		\
	}

#undef CheckCondition
#define CheckCondition( value, ret )	\
	if( !(value) )	\
		return (ret)

#undef CheckConditionVoid
#define CheckConditionVoid( value )		\
	if( !(value) )	\
		return 


#undef ARRAY_SIZE
#define ARRAY_SIZE( a )		\
  ((sizeof( a ) / sizeof( *(a) )) / \
   static_cast< uint32 >( !(sizeof( a ) % sizeof( *(a) )) ))

#undef DELETE_VALUE
#define DELETE_VALUE( value )		\
	{ delete (value); (value) = NULL; }

#undef DELETE_ARRAY
#define DELETE_ARRAY( value )	\
	{ delete [] (value); value = NULL; }

namespace internal {

// Use implicit_cast as a safe version of static_cast or const_cast
// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
// to a pointer to SuperclassOfFoo or casting a pointer to Foo to
// a const pointer to Foo).
// When you use implicit_cast, the compiler checks that the cast is safe.
// Such explicit implicit_casts are necessary in surprisingly many
// situations where C++ demands an exact type match instead of an
// argument type convertable to a target type.
//
// The From type can be inferred, so the preferred syntax for using
// implicit_cast is the same as for static_cast etc.:
//
//   implicit_cast<ToType>(expr)
//
// implicit_cast would have been part of the C++ standard library,
// but the proposal was submitted too late.  It will probably make
// its way into the language in the future.
template < typename To, typename From >
inline To implicit_cast( From const &f ) 
{
	return f;
}

}  // namespace internal

// We made these internal so that they would show up as such in the docs,
// but we don't want to stick "internal::" in front of them everywhere.
using internal::implicit_cast;


/*namespace internal {

template < bool C > 
struct Assert 
{
};

template <>
struct Assert< true > 
{
	typedef int Type;
};

} // namespace internal

#undef COMPILE_ASSERT
#define COMPILE_ASSERT( expr, msg )		\
	sizeof( typename internal::Assert< bool( expr ) >::Type )
*/

namespace internal {

template < bool >
struct CompileAssert {};

}  // namespace internal

#undef COMPILE_ASSERT
#define COMPILE_ASSERT( expr, msg ) \
	typedef internal::CompileAssert< (bool(expr)) > \
		msg[bool(expr) ? 1 : -1]


// double to long
union DoubleCast { double l_d; long l_l; };
#define Double2Long( i, d )  { volatile union DoubleCast u; \
   u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }


#undef min
#undef max

template < typename T> 
inline static T Min( const T &a, const T &b ) 
{ 
	return (b < a) ? b : a; 
}

template < typename T> 
inline static T Max( const T &a, const T &b ) 
{ 
	return (a < b) ? b : a; 
}

#define FMT_STRING( buffer, size, pat )	\
	do	\
	{	\
		va_list vl;		\
		memset( buffer, 0, size );		\
		va_start( vl, pat );	\
		vsnprintf( buffer, size - 1, pat, vl );		\
		va_end( vl );	\
	}	\
	while( false )

#ifndef _DEBUG_LINBO
	#define _DEBUG_LINBO
#endif

#endif

